package com.example.ssm.controller;

import com.example.ssm.QueryVO;
import com.example.ssm.pojo.BaseDict;
import com.example.ssm.pojo.Customer;
import com.example.ssm.service.IBaseDictService;
import com.example.ssm.service.ICustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    // 客户来源
    @Value("${CUSTOMER_FROM_TYPE}")
    private String CUSTOMER_FROM_TYPE;
    // 客户行业
    @Value("${CUSTOMER_INDUSTRY_TYPE}")
    private String CUSTOMER_INDUSTRY_TYPE;
    // 客户级别
    @Value("${CUSTOMER_LEVEL_TYPE}")
    private String CUSTOMER_LEVEL_TYPE;


    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IBaseDictService baseDictService;

    @RequestMapping("/list")
    public String list(Model model, QueryVO queryVO){

        // 客户来源
        List<BaseDict> fromType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_FROM_TYPE);
        // 所属行业
        List<BaseDict> industryType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_INDUSTRY_TYPE);
        // 客户级别
        List<BaseDict> levelType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_LEVEL_TYPE);
        //客户列表
        List<Customer> customers=customerService.list(queryVO);

        // 把前端页面需要显示的数据放到模型中
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);
        model.addAttribute("customers",customers);

        return "customer";
    }

    @RequestMapping("/list_bak")
    public String list_bak(Model model, QueryVO queryVO){

        // 客户来源
        List<BaseDict> fromType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_FROM_TYPE);
        // 所属行业
        List<BaseDict> industryType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_INDUSTRY_TYPE);
        // 客户级别
        List<BaseDict> levelType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_LEVEL_TYPE);
        //客户列表
        /*List<Customer> customers=customerService.list(queryVO);*/
        PageInfo<Customer> pageInfo=customerService.list_bak(queryVO);
        System.out.println(pageInfo);

        // 把前端页面需要显示的数据放到模型中
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);
        /*model.addAttribute("customers",customers);*/
        model.addAttribute("pageInfo",pageInfo);

        return "customer_bak";
    }

    //增加
    @RequestMapping("/add")
    @ResponseBody
    public String add(Customer customer)
    {
        System.out.println("customer:"+customer);
        customerService.addCustomer(customer);
        return "{\"result\":true}";
    }
    //查询
    @RequestMapping("/edit")
    @ResponseBody
    public Customer edit(long id)
    {
        return customerService.queryCustomerById(id);
    }
    //修改
    @RequestMapping("/update")
    @ResponseBody
    public String update(Customer customer)
    {
        System.out.println("customer:"+customer);
        customerService.updateCustomerById(customer);
        return "{\"result\":true}";
    }
    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(long id)
    {
        customerService.deleteCustomerById(id);
        return "{\"result\":true}";
    }
}
